define([
    'app',
    'underscore'
], function(app, _) {
    app.controller('SQLMapperController', function Controller($window, SqlMapperService) {
        var that = this;
        
        that.params = {
            host: '',
            port: null,
            database: '',
            table: '',
            user: '',
            password: ''
        };
        
        that.sqlMapper = '';
        that.template = [
            '<%',
            'var noPkColumns = _.filter(columns, function(column) {',
            '    return _.find(primaryKeys, function(primaryKey) {',
            '        return column.jdbcName == primaryKey.jdbcName;',
            '    }) == undefined;',
            '});',
            '%>',
            '<select id="select" parameterType="" resultType="">',
            '    SELECT <%_.each(columns, function(column, index) {%><%if (index > 0) {%>, <%}%><%=column.jdbcName%><%});%>',
            '    FROM <%=name%>',
            '    <where>',
            '    <%_.each(noPkColumns, function(column) {%>',
            '        <if test="<%=column.javaName%> != null">',
            '            AND <%=column.jdbcName%> = #{<%=column.javaName%>,jdbcType=<%=column.jdbcType%>}',
            '        </if>',
            '    <%});%>',
            '    </where>',
            '</select>',
            '<insert id="insert" parameterType="" useGeneratedKeys="true" keyProperty="">',
            '    INSERT INTO <%=name%>',
            '    <trim prefix="(" suffix=")" suffixOverrides=",">',
            '    <%_.each(columns, function(column) {%>',
            '        <%if (column.generated) { return; }%>',
            '        <%if (column.nullable || column.defaults != null) {%>',
            '        <if test="<%=column.javaName%> != null">',
            '            <%=column.jdbcName%>,',
            '        </if>',
            '        <%} else {%>',
            '        <%=column.jdbcName%>,',
            '        <%}%>',
            '    <%});%>',
            '    </trim>',
            '    <trim prefix="VALUES (" suffix=")" suffixOverrides=",">',
            '    <%_.each(columns, function(column) {%>',
            '        <%if (column.generated) { return; }%>',
            '        <%if (column.nullable || column.defaults != null) {%>',
            '        <if test="<%=column.javaName%> != null">',
            '            #{<%=column.javaName%>,jdbcType=<%=column.jdbcType%>},',
            '        </if>',
            '        <%} else {%>',
            '        #{<%=column.javaName%>,jdbcType=<%=column.jdbcType%>},',
            '        <%}%>',
            '    <%});%>',
            '    </trim>',
            '</insert>',
            '<update id="update" parameterType="">',
            '    UPDATE <%=name%>',
            '    <set>',
            '    <%_.each(noPkColumns, function(column) {%>',
            '        <if test="<%=column.javaName%> != null">',
            '            <%=column.jdbcName%> = #{<%=column.javaName%>,jdbcType=<%=column.jdbcType%>},',
            '        </if>',
            '    <%});%>',
            '    </set>',
            '    WHERE <%_.each(primaryKeys, function(primaryKey, index) {%><%if (index > 0) {%> AND <%}%><%=primaryKey.jdbcName%> = #{<%=primaryKey.javaName%>,jdbcType=<%=primaryKey.jdbcType%>}<%});%>',
            '</update>',
            '<delete id="delete" parameterType="">',
            '    DELETE FROM <%=name%> WHERE <%_.each(primaryKeys, function(primaryKey, index) {%><%if (index > 0) {%> AND <%}%><%=primaryKey.jdbcName%> = #{<%=primaryKey.javaName%>,jdbcType=<%=primaryKey.jdbcType%>}<%});%>',
            '</delete>'].join('\n');
        
        that.init = function() {
            new Clipboard('#copy');
        };

        that.generateSqlMapper = function(valid) {
            if (valid) {
                SqlMapperService.getTable(that.params).then(function(resp) {
                    var table = resp.data.data;
                    console.log("Table Data => %o", table);
                    if (table) {
                        var template = _.template(that.template);
                        that.sqlMapper = template(table).replace(/^[ \t]*\n/gm, '');
                    }
                });
            }
        }

        that.clearAll = function() {
            that.params = {
                host: '',
                port: null,
                database: '',
                table: '',
                user: '',
                password: ''
            };
            that.sqlMapper = '';
        };
    });
});
